home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 76 / DVD Actual 1 Marzo 2003.iso / Trial / TurboCAD 7.1 Pro / Data.Cab / F25103_ViewWnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  12.5 KB  |  629 lines

  1. // ViewWnd.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "tcintf.h"
  6. #include "InsTool.h"
  7. #include "InsSymb.h"
  8. #include "ViewWnd.h"
  9. #include "TDialog.h"
  10.  
  11.  
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CViewWnd
  21.  
  22. //CViewWnd::CViewWnd(CInsSymb* pTool)
  23. CViewWnd::CViewWnd(CInsSymb* pTool)
  24. {
  25.     EnableAutomation();
  26.     m_FirstStart = TRUE;
  27.     m_pIApp = NULL;
  28.     m_pDwgs = NULL;
  29.     m_pPreviewDrawing = NULL;
  30.     m_pPreviewGrs = NULL;
  31.     m_pPreviewView = NULL; 
  32.     m_pViewsCol = NULL;
  33.     m_hCurActDr = NULL;
  34.     
  35.     {
  36.         m_pTool = pTool;
  37.     }
  38. }
  39.  
  40. CViewWnd::~CViewWnd()
  41. {
  42. }
  43.  
  44. void CViewWnd::OnFinalRelease()
  45. {
  46.     // When the last reference for an automation object is released
  47.     // OnFinalRelease is called.  The base class will automatically
  48.     // deletes the object.  Add additional cleanup required for your
  49.     // object before calling the base class.
  50.  
  51.     CWnd::OnFinalRelease();
  52. }
  53.  
  54.  
  55. BEGIN_MESSAGE_MAP(CViewWnd, CWnd)
  56.     //{{AFX_MSG_MAP(CViewWnd)
  57.     ON_WM_PAINT()
  58.     ON_WM_ERASEBKGND()
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61.  
  62.  
  63. BEGIN_DISPATCH_MAP(CViewWnd, CWnd)
  64.     //{{AFX_DISPATCH_MAP(CViewWnd)
  65.         // NOTE - the ClassWizard will add and remove mapping macros here.
  66.     //}}AFX_DISPATCH_MAP
  67. END_DISPATCH_MAP()
  68.  
  69. // Note: we add support for IID_IViewWnd to support typesafe binding
  70. //  from VBA.  This IID must match the GUID that is attached to the 
  71. //  dispinterface in the .ODL file.
  72.  
  73. // {4B1AA263-851C-11D2-8BEA-00403338C504}
  74. static const IID IID_IViewWnd =
  75. { 0x4b1aa263, 0x851c, 0x11d2, { 0x8b, 0xea, 0x0, 0x40, 0x33, 0x38, 0xc5, 0x4 } };
  76. //const CLSID CLSID_XApplication = {0x6A481801,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  77.  
  78.  
  79. const IID IID_ITcApp =  {0x6A481200,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  80. const IID IID_ITcDrawing = {0x6A481201,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  81. //const IID IID_IDrawing = {0x6A481107,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  82.  
  83. BEGIN_INTERFACE_MAP(CViewWnd, CWnd)
  84.     INTERFACE_PART(CViewWnd, IID_IViewWnd, Dispatch)
  85. END_INTERFACE_MAP()
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CViewWnd message handlers
  89.  
  90. BOOL CViewWnd::CreatePreview()
  91. {
  92.     HRESULT hRes = E_FAIL;
  93.     COleVariant varType((const long)imsiPolyline);
  94.     COleVariant varOptional(varMissing);
  95.  
  96.  
  97.     try
  98.     {    
  99.         ClearAll();
  100.         HMODULE    hDBAPI = NULL;// = GetModuleHandle(DBAPI50);
  101.         hDBAPI = GetModuleHandle(DBAPI70);
  102.         if (hDBAPI == NULL)
  103.         {
  104.             hDBAPI = GetModuleHandle(DBAPI60);
  105.  
  106.             if (hDBAPI == NULL)
  107.             {
  108.                 hDBAPI = GetModuleHandle(DBAPI50);
  109.             }        
  110.         }
  111.  
  112.         if (hDBAPI == NULL)
  113.             return FALSE;
  114.         else 
  115.         m_pfGetApp = (APPGETCURRENTAPP)GetProcAddress(hDBAPI, _T("AppGetCurrentApp"));
  116.         if (m_pfGetApp == NULL)                                                    
  117.             return FALSE;
  118.  
  119.         m_lApp = (*m_pfGetApp)();
  120.         if (m_lApp == 0)
  121.             return FALSE;
  122.             
  123.         m_pfFileOpen = (GRAPHICFILEOPENEX)GetProcAddress(hDBAPI, _T("GraphicFileOpenEx"));
  124.         if (m_pfFileOpen == NULL)                                                    
  125.             return FALSE;
  126.  
  127.         m_pfAppDwgNew = (APPDRAWINGNEWEX)GetProcAddress(hDBAPI, _T("AppDrawingNewEx"));
  128.         if (m_pfAppDwgNew == NULL)                                                    
  129.             return FALSE;
  130.  
  131.         BOOL i;
  132.         i = TRUE;
  133.         COleVariant varItem = (0L);
  134.         long nDrawings = 0; 
  135.  
  136.         m_lDwg = (*m_pfAppDwgNew)(m_lApp, i, NULL, NULL);
  137.  
  138.         hRes = ((CTDialog*)GetParent())->m_pThisTool->get_Application(&m_pIApp);                
  139.         CHECK_HRESULT(hRes)
  140.  
  141.         hRes = m_pIApp->get_Drawings(&m_pDwgs);
  142.         CHECK_HRESULT(hRes)
  143.  
  144.         hRes = m_pDwgs->get_Count(&nDrawings);
  145.         CHECK_HRESULT(hRes)
  146.             
  147.         varItem = nDrawings - 1L;
  148.             
  149.         hRes = m_pDwgs->get_Item(&varItem, &m_pPreviewDrawing);
  150.         CHECK_HRESULT(hRes)
  151.             
  152.         hRes = m_pPreviewDrawing->get_Views(&m_pViewsCol);
  153.         CHECK_HRESULT(hRes)
  154.             
  155.             
  156.         HWND hWnd = ::GetDlgItem(GetParent()->m_hWnd, IDC_PREVIEW);
  157.         COleVariant varHwnd((long)hWnd);
  158.         long nViews = 0;
  159.  
  160.         hRes = m_pViewsCol->Add(&varHwnd, &varOptional, &m_pPreviewView );
  161.         CHECK_HRESULT(hRes)
  162.             
  163.         hRes = m_pPreviewView->put_Update(FALSE);
  164.         CHECK_HRESULT(hRes)
  165.             
  166.  
  167.         hRes = m_pPreviewView->put_MappingMode(MM_TEXT);
  168.         CHECK_HRESULT(hRes)
  169.  
  170.         hRes = m_pPreviewView->put_FixedAspectRatio(TRUE);
  171.         CHECK_HRESULT(hRes)
  172.     
  173.         hRes = m_pPreviewView->put_Margins(FALSE);
  174.         CHECK_HRESULT(hRes)
  175.  
  176.         CViewWnd *pPreview = (CViewWnd *) (((CTDialog*)GetParent())->GetDlgItem(IDC_PREVIEW));
  177.  
  178.         AFX_MANAGE_STATE(m_pTool->m_pTCState);
  179.  
  180.         CRect rcBounds;
  181.         pPreview->GetClientRect(&rcBounds);
  182.         pPreview->Invalidate(); 
  183.  
  184.         hRes = m_pPreviewView->put_ScreenLeft(rcBounds.left);
  185.         CHECK_HRESULT(hRes)
  186.  
  187.         hRes = m_pPreviewView->put_ScreenTop(rcBounds.top);
  188.         CHECK_HRESULT(hRes)
  189.  
  190.         hRes = m_pPreviewView->put_ScreenWidth(rcBounds.Width());
  191.         CHECK_HRESULT(hRes)
  192.  
  193.         hRes = m_pPreviewView->put_ScreenHeight(rcBounds.Height());
  194.         CHECK_HRESULT(hRes)
  195.  
  196.         hRes = m_pPreviewView->ZoomToExtents();
  197.         CHECK_HRESULT(hRes)
  198.  
  199.         VariantClear(&varType);
  200.         VariantClear(&varOptional);
  201.         
  202.     }
  203.     catch (...)
  204.     {
  205.  
  206.         TRACE_EXCEPTION("CViewWnd::CreatePreview")
  207.         ClearAll();
  208.         _clearfp();
  209.  
  210.     }
  211.     if(m_pIApp != NULL)
  212.     {
  213.         m_pIApp->Release();
  214.         m_pIApp = NULL;
  215.     }
  216.     return TRUE;
  217. }
  218.  
  219. BOOL CViewWnd::DoPreview()
  220. {
  221.     HRESULT hRes = E_FAIL;
  222.     BOOL bRes = FALSE;
  223. //    COleVariant varType((const long)imsiPolyline);
  224.     COleVariant varType((const long)imsiGroup);
  225.     COleVariant varOptional(varMissing);
  226.     CString cstrError;
  227.     BOOL curMode;
  228.     BOOL i;
  229.     i = TRUE;
  230.     long IDwg1 = 0;
  231.     COleVariant varItem = (0L);
  232.     long nDrawings = 0; 
  233.     IDrawing* pTmpDwg = NULL;
  234.  
  235.     Properties *pProps = NULL;
  236.     Property *pProp = NULL;
  237.     Properties *pProps1 = NULL;
  238.     Property *pProp1 = NULL;
  239.     CMerge merge;
  240.     IGraphic* pIGrTmp = NULL;
  241.     IGraphic* pIGrTmp1 = NULL;
  242.  
  243.     ITcDrawing* pITcDr = NULL;
  244.     ITcApp* pTcApp = NULL;
  245.     IDrawing* pIDwg = NULL;
  246.  
  247.     try
  248.     {
  249.         CButton *pCheckBox = (CButton *) (GetParent()->GetDlgItem(IDC_CHECK1));
  250.  
  251.         
  252.         CToolList *pList = (CToolList *) (GetParent()->GetDlgItem(IDC_LIST1));
  253.  
  254.         int index = pList->GetCurSel();
  255.  
  256.         pList->GetText(index, m_selected);
  257.  
  258.         IDwg1 = (*m_pfAppDwgNew)(m_lApp, i, NULL, NULL);
  259.             
  260.         hRes = m_pDwgs->get_Count(&nDrawings);
  261.         CHECK_HRESULT(hRes)
  262.             
  263.         varItem = nDrawings - 1L;
  264.             
  265.         hRes = m_pDwgs->get_Item(&varItem, &pTmpDwg);
  266.         CHECK_HRESULT(hRes)
  267.  
  268.         AFX_MANAGE_STATE(m_pTool->m_pTCState);
  269.  
  270.         DWORD dwRes = (*m_pfFileOpen)(0, IDwg1, m_lApp, (LPTSTR)(LPCSTR)m_selected, NULL, cstrError.GetBuffer(256), 255,  GF_OPENMODE, 0, NULL );
  271.  
  272.         if (dwRes == NULL)
  273.             return FALSE;
  274.         
  275.         CString cstrName("TileMode");//# Non-localizable string#
  276.         BSTR bstrName = cstrName.AllocSysString();
  277.  
  278.       ::VariantInit(&varItem);
  279.         varItem.vt = VT_BSTR;
  280.         varItem.bstrVal = bstrName;
  281.  
  282.         hRes = pTmpDwg->get_Properties(&pProps);
  283.         CHECK_HRESULT(hRes)
  284.  
  285.         hRes = pProps->get_Item(&varItem, &pProp);
  286.         CHECK_HRESULT(hRes)
  287.  
  288.         hRes = m_pPreviewDrawing->get_Properties(&pProps1);
  289.         CHECK_HRESULT(hRes)
  290.  
  291.         hRes = pProps1->get_Item(&varItem, &pProp1);
  292.         CHECK_HRESULT(hRes)
  293.  
  294.         COleVariant varVal;//(0L);
  295.         varVal.vt = VT_I2;
  296.  
  297.         pProp->get_Value(NULL, &varVal);
  298.         long dd = varVal.iVal;
  299.  
  300.         if (varVal.iVal == 1)
  301.         {
  302.             curMode = 1;//imsiModelSpace;
  303.         }
  304.         else 
  305.         {
  306.             curMode = 0;//imsiPaperSpace;
  307.         
  308.         }
  309.  
  310.  
  311.  
  312. // switch the Preview drawing and temp drawing to paper space. 
  313.         varVal.iVal = 0;
  314.         hRes = pProp1->put_Value(NULL, &varVal);
  315.         CHECK_HRESULT(hRes)
  316.  
  317.         hRes = pProp->put_Value(NULL, &varVal);
  318.         CHECK_HRESULT(hRes)
  319.  
  320.         BOOL bGrsOnly;
  321.         bGrsOnly = FALSE;
  322. // merge the paper space graphics collection and blocks, styles, layers...    
  323.         hRes = merge.Init(pTmpDwg, m_pPreviewDrawing, FALSE);
  324.         CHECK_HRESULT(hRes)
  325.         
  326.         hRes = merge.Run(&pIGrTmp, bGrsOnly);
  327.         CHECK_HRESULT(hRes)
  328.  
  329.             
  330. // switch the Preview drawing and temp drawing to model space. 
  331.       
  332.         varVal.iVal = 1;
  333.  
  334.         hRes = pProp1->put_Value(NULL, &varVal);
  335.         CHECK_HRESULT(hRes)
  336.  
  337.         hRes = pProp->put_Value(NULL, &varVal);
  338.         CHECK_HRESULT(hRes)
  339.  
  340.         // merge the model space graphics collection    
  341.  
  342. //        bGrsOnly = TRUE;
  343.         bGrsOnly = FALSE;
  344.         hRes = merge.Init(pTmpDwg, m_pPreviewDrawing, FALSE);
  345.         CHECK_HRESULT(hRes)
  346.  
  347.         hRes = merge.Run(&pIGrTmp1, bGrsOnly);
  348.         CHECK_HRESULT(hRes)
  349.  
  350.  
  351.  
  352. // set the space mode for Preview drawing the same like in file that is open.
  353.         if (curMode == 0) //imsiPaperSpace 
  354.         {
  355.             varVal.iVal = 0;
  356.  
  357.             hRes = pProp1->put_Value(NULL, &varVal);
  358.             CHECK_HRESULT(hRes)
  359.  
  360.         }
  361.         else
  362.         {
  363.             
  364.             //    curMode = imsiModelSpace;
  365.             varVal.iVal = 1;
  366.  
  367.             hRes = pProp1->put_Value(NULL, &varVal);
  368.             CHECK_HRESULT(hRes)
  369.  
  370.         }
  371.  
  372.  
  373. //        hRes = ((CTDialog*)GetParent())->m_pRegMets->get_Application(&m_pIApp);
  374. //        CHECK_HRESULT(hRes)
  375.  
  376.         hRes = m_pPreviewDrawing->get_Application(&m_pIApp);
  377.         CHECK_HRESULT(hRes)
  378.     
  379.  
  380.         hRes = m_pIApp->QueryInterface(IID_ITcApp, (void **) &pTcApp); 
  381.         CHECK_HRESULT(hRes)
  382.             
  383.         hRes = pTcApp->GetActiveDrawing(&pITcDr);
  384.         CHECK_HRESULT(hRes)
  385.             
  386.                 
  387.         hRes = pITcDr->QueryInterface(IID_IDrawing, (void **) &pIDwg); 
  388.         CHECK_HRESULT(hRes)
  389.  
  390.         hRes = pIDwg->get__Handle((long*)&m_hCurActDr);
  391.         CHECK_HRESULT(hRes)
  392.  
  393.         if (pProp != NULL)
  394.         {
  395.             pProp->Release();
  396.             pProp = NULL;
  397.         }
  398.  
  399.         if (pProps != NULL)
  400.         {
  401.             pProps->Release();
  402.             pProps = NULL;
  403.         }
  404.  
  405.         if(pIGrTmp !=NULL)
  406.         {
  407.             pIGrTmp->Release();
  408.             pIGrTmp = NULL;
  409.         }
  410.  
  411.         COleVariant varOptional(varMissing);
  412.         pTmpDwg->Close(varOptional, varOptional, varOptional);
  413.  
  414.         if (m_hCurActDr != NULL)
  415.         {
  416.             hRes = m_pIApp->QueryInterface(IID_ITcApp, (void **) &pTcApp); 
  417.             CHECK_HRESULT(hRes)
  418.  
  419.             hRes = pTcApp->SetActiveDrawing(m_hCurActDr, NULL);
  420.             CHECK_HRESULT(hRes)
  421.  
  422.             m_hCurActDr = NULL;
  423.         }
  424.             
  425.  
  426.         hRes = m_pPreviewView->ZoomToExtents();
  427.         CHECK_HRESULT(hRes)
  428. /*
  429.         hRes = m_pPreviewView->Refresh();
  430.         CHECK_HRESULT(hRes)
  431. */
  432.         if(pCheckBox == NULL)
  433.             return FALSE;
  434.  
  435.         if (curMode == 1) //imsiModelSpace 
  436.             pCheckBox->SetCheck(1);
  437.  
  438.         else
  439.             pCheckBox->SetCheck(0);
  440.  
  441.         bRes = TRUE;
  442.  
  443.     }
  444.     catch (...)
  445.     {
  446.         _clearfp();
  447.         TRACE_EXCEPTION("CViewWnd::DoPreview")
  448.         hRes = E_FAIL;
  449.     
  450.     }
  451.  
  452.     if(pIDwg !=NULL)
  453.     {
  454.         pIDwg->Release();
  455.         pIDwg = NULL;
  456.     }
  457.         
  458.     if(pITcDr != NULL)
  459.     {
  460.         pITcDr->Release();
  461.         pITcDr = NULL;
  462.     }
  463.     if(pTcApp != NULL)
  464.     {
  465.         pTcApp->Release();
  466.         pTcApp = NULL;
  467.     }
  468.  
  469. /*    
  470.     if(pIGrTmp1 !=NULL)
  471.     {
  472.         pIGrTmp1->Release();
  473.         pIGrTmp1 = NULL;
  474.     }
  475. */
  476. /*
  477.     if (pProp != NULL)
  478.     {
  479.         pProp->Release();
  480.         pProp = NULL;
  481.     }
  482.  
  483.     if (pProps != NULL)
  484.     {
  485.         pProps->Release();
  486.         pProps = NULL;
  487.     }
  488.  
  489.     if(pIGrTmp !=NULL)
  490.     {
  491.         pIGrTmp->Release();
  492.         pIGrTmp = NULL;
  493.     }
  494. */
  495.     if (pProp1 != NULL)
  496.     {
  497.         pProp1->Release();
  498.         pProp1 = NULL;
  499.     }
  500.     if (pProps1 != NULL)
  501.     {
  502.         pProps1->Release();
  503.         pProps1 = NULL;
  504.     }
  505.  
  506.     if(pTmpDwg != NULL)
  507.     {
  508.         pTmpDwg->Release();
  509.         pTmpDwg = NULL;
  510.     }
  511.     if(pTcApp != NULL)
  512.     {
  513.         pTcApp->Release();
  514.         pTcApp = NULL;
  515.     }
  516.     if(m_pIApp != NULL)
  517.     {
  518.         m_pIApp->Release();
  519.         m_pIApp = NULL;
  520.     }
  521.  
  522.     return bRes;
  523.  
  524. }
  525.  
  526. void CViewWnd::RefreshList()
  527. {
  528.  
  529. }
  530.  
  531. void CViewWnd::Clear()
  532. {
  533.     Invalidate();
  534. }
  535.  
  536. void CViewWnd::ClearAll()
  537. {
  538.     try
  539.     {
  540.  
  541.  
  542.         if (m_pPreviewGrs != NULL)
  543.         {
  544.             m_pPreviewGrs->Release();
  545.             m_pPreviewGrs = NULL;
  546.  
  547.         }
  548.         if (m_pPreviewView != NULL)
  549.         {
  550.             m_pPreviewView->Delete();
  551.             m_pPreviewView->Release();
  552.             m_pPreviewView = NULL;
  553.         
  554.         }
  555.         if (m_pViewsCol != NULL)
  556.         {
  557.             m_pViewsCol->Release();
  558.             m_pViewsCol = NULL;
  559.  
  560.         }
  561.         if (m_pPreviewDrawing != NULL)
  562.         {
  563.  
  564.             COleVariant varOptional(varMissing);
  565.             HRESULT hRes = m_pPreviewDrawing->Close(varOptional, varOptional, varOptional);
  566.             CHECK_HRESULT(hRes)
  567.             m_pPreviewDrawing->Release();
  568.             m_pPreviewDrawing = NULL;
  569.         }
  570.  
  571.         m_FirstStart = TRUE;
  572.     }
  573.  
  574.     catch (...)
  575.     {
  576.         _clearfp();
  577.         TRACE_EXCEPTION("CViewWnd::ClearAll")
  578.     }
  579.  
  580.     if (m_pDwgs != NULL)
  581.     {
  582.         m_pDwgs->Release();
  583.         m_pDwgs = NULL;
  584.     }
  585.  
  586.     if (m_pIApp != NULL)
  587.     {
  588.         m_pIApp->Release();
  589.         m_pIApp = NULL;
  590.     }
  591.  
  592. }
  593.  
  594. void CViewWnd::OnPaint() 
  595. {
  596.     CPaintDC dc(this); // device context for painting
  597.     
  598.     // TODO: Add your message handler code here
  599.     if (m_pPreviewView == NULL)
  600.         return;
  601.  
  602.     // Force redraw of view window
  603.     try
  604.     {
  605.         HRESULT hRes = m_pPreviewView->Refresh();
  606.         CHECK_HRESULT(hRes)
  607.  
  608.     }
  609.     catch (...)
  610.     {
  611.         _clearfp();
  612.         TRACE_EXCEPTION("CViewWnd::Paint")
  613.     }
  614.     
  615.     // Do not call CWnd::OnPaint() for painting messages
  616. }
  617.  
  618. BOOL CViewWnd::OnEraseBkgnd(CDC* pDC) 
  619. {
  620.     // TODO: Add your message handler code here and/or call default
  621.     CRect rect;
  622.     GetClientRect(rect);
  623.     pDC->FillSolidRect(rect, ::GetSysColor(COLOR_WINDOW));
  624.     return TRUE;
  625.     
  626. //    return CWnd::OnEraseBkgnd(pDC);
  627. }
  628.  
  629.